package com.scalosoft.action.serviziostep;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import com.scalosoft.action.step.Allegato;

@SuppressWarnings("serial")
@Namespace(value = "/serviziostep")

public class ServizioStepAction extends ActionSupport implements SessionAware , Preparable {
	
	private static final Log log = LogFactory.getLog(ServizioStepAction.class);
	private static final String STEP = "STEP";
	private static final String ISTANZA = "ISTANZA";
	private static String LISTA_ALLEGATI = "LISTA_ALLEGATI";
	
	//							 #0		   					#1							#2					#3
	public String listaStep[]={"/serviziostep/step-0","/serviziostep/step-1","/upload/UploadList","/serviziostep/step-3"};
	public Integer step;
	
	private Istanza istanza = null;
	private List<Allegato> allegati = null;
	
	private ArrayList<ChiaveValore> listaTipoPagamento;
	
	public ArrayList<ChiaveValore> getListaTipoPagamento() {
		return listaTipoPagamento;
	}
	
	private Map<String, Object> session ;
	
	private String backStep;
	private String nextStep;
	private String curStep;
	
	@Override
	public void setSession(Map<String, Object> session) {
        this.session = session ;
    }
	
	@Override
	public void prepare() throws Exception {
		step = (Integer) session.get(STEP);
		istanza = (Istanza) session.get(ISTANZA);
		listaTipoPagamento = new ArrayList<ChiaveValore>();
		listaTipoPagamento.add(new ChiaveValore("rid","Rid"));
		listaTipoPagamento.add(new ChiaveValore("cc","CC"));
		listaTipoPagamento.add(new ChiaveValore("pp","Paypal"));
		listaTipoPagamento.add(new ChiaveValore("ccr","Carta di credito"));
	}

	@VisitorFieldValidator
	public Istanza getIstanza() {
		return istanza;
	}

	public void setIstanza(Istanza istanza) {
		this.istanza = istanza;
	}
	
	@Action(value = "next", results = { 
			@Result(name = "success", location = "${nextStep}", type = "redirect", params = {"nextStep" ,"%{listaStep[step+1]}","backStep" ,"%{listaStep[step-1]}","curStep" ,"%{listaStep[step]}"}),
			@Result(name = "input", location = "${curStep}", type = "redirect") 
	})
	public String next() {
		log.debug("* next");
		step++;
		log.debug("step-"+step+" action");
//		log.debug("nextStep :" + nextStep);
//		log.debug("backStep :" + backStep);
//		log.debug("curStep :" + curStep);
		log.debug("istanza :" + istanza);
		session.put(STEP,step);
		session.put(ISTANZA, istanza);
		return "success";
	}
	
	@Action(value = "back", results = { 
			@Result(name = "success", location = "${backStep}", type = "redirect" , params = {"nextStep" ,"%{listaStep[step+1]}","backStep" ,"%{listaStep[step-1]}","curStep" ,"%{listaStep[step]}"})
	})
	public String back() {
		log.debug("* back");
		step--;
		log.debug("step-"+step+" action");
		log.debug("nextStep :" + nextStep);
		log.debug("backStep :" + backStep);
		log.debug("curStep :" + curStep);
		log.debug("istanza :" + istanza);
		session.put(STEP,step);
		session.put(ISTANZA, istanza);
		return "success";
	}
	
	@Action(value = "step-0" , results={
			@Result(name = "success", location = "step-0.jsp"),
			@Result(name = "input", location = "step-0.jsp")
	})
	public String step0() {
		//step=getStepIndex("/serviziostep/step-0");
		log.debug("* step-"+step+" action");
		log.debug("istanza :" + istanza);
		session.put(STEP,step);
		session.put(ISTANZA, istanza);
		return SUCCESS;
	}
	
	@Action(value = "step-1" , results={
			@Result(name = "success", location = "step-1.jsp"),
			@Result(name = "input", location = "step-1.jsp")
	})
	public String step1() {
		//step=getStepIndex("/serviziostep/step-1");
		log.debug("* step-"+step+" action");
		log.debug("istanza :" + istanza);
		session.put(STEP,step);
		session.put(ISTANZA, istanza);
		return SUCCESS;
	}
	
	/*
	@SkipValidation
	@Action(value = "step-1-reload" , results={
			@Result(name = "success", location = "step-1.jsp"),
	})
	public String step1Reload() {

		return SUCCESS;
	}
	
	@Action(value = "step-upload" , results={
			@Result(name = "success", location="/upload/UploadList",  type="redirect", params = {"nextUrl" ,"/step/step-3","backUrl" ,"/step/step-2"} ) 
	})
	public String stepUpload() {
		return SUCCESS;
	}
	*/
	
	@Action(value = "step-3" , results={
			@Result(name = "success", location = "step-3.jsp"),
			@Result(name = "input", location = "step-3.jsp")
	})
	public String step3() {
		//step=getStepIndex("/serviziostep/step-3");
		// ALLEGATI
		allegati = (List<Allegato>) session.get(LISTA_ALLEGATI);
		log.debug("* step-"+step+" action");
		log.debug("istanza :" + istanza);
		session.put(STEP,step);
		session.put(ISTANZA, istanza);
		return SUCCESS;
	}
	
	private int getStepIndex(String step){
		int idx = 0;
		for(String s: listaStep){
			if(step.equals(s))
				return idx;
			if(idx<listaStep.length) idx++;
			else idx=-1;
		}
		return idx;
	}
	
	@SkipValidation
	@Action(value = "init", results = { @Result(name = "success", location = "step-0.jsp") })
	public String init() {
		step = 0;
		session.put(STEP,step);
		session.put(ISTANZA,new Istanza());
		return SUCCESS;
	}

	public String getBackStep() {
		return backStep;
	}

	public void setBackStep(String backStep) {
		this.backStep = backStep;
	}

	public String getNextStep() {
		return nextStep;
	}

	public void setNextStep(String nextStep) {
		this.nextStep = nextStep;
	}

	public String getCurStep() {
		return curStep;
	}

	public void setCurStep(String curStep) {
		this.curStep = curStep;
	}

	public List<Allegato> getAllegati() {
		return allegati;
	}

	public void setAllegati(List<Allegato> allegati) {
		this.allegati = allegati;
	}
	
}
